using Microsoft.AspNetCore.Mvc;
using Infrastructure.Filters;
using ZR.Infrastructure;
using ZR.Service.Dev.IService;
using ZR.Model.Dev.Dto;
using ZR.Model.Dev.Entity;
using ZR.Model.Dev;
using ZR.Model;
using ZR.ServiceCore.Attributes;
using ZR.Admin.WebApi.MainModules;

namespace ZR.Admin.WebApi.Controllers.Dev
{
    /// <summary>
    /// 系统菜单
    /// </summary>
    [Route("dev/menu")]
    [ApiController]
    [EnableRspContract]
    [ModuleInfo(typeof(DevModule),"系统菜单管理")]
    public partial class MenuController : ApiControllerBase
    {

        /// <summary>
        /// 系统菜单接口
        /// </summary>
        private readonly IMenuService _MenuService;

        private readonly MapsterAdaptEntityCfg _mapsterAdaptEntityCfg;

        public MenuController(IMenuService menuService, MapsterAdaptEntityCfg mapsterAdaptEntityCfg)
        {
            _MenuService = menuService;
            _mapsterAdaptEntityCfg = mapsterAdaptEntityCfg;
        }

        /// <summary>
        /// 添加系统菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost("add")]
        [ActionPermissionFilter(Permission = "menu:add")]
        [Log(Title = "系统菜单", BusinessType = BusinessType.INSERT)]
        public async Task AddMenu([FromBody] MenuFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<MenuEntity>(_mapsterAdaptEntityCfg).ToCreate(HttpContext, EntityExtension.AutoFillField2);
            var isOk = await _MenuService.AddMenu(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "添加失败");
            }
        }

        /// <summary>
        /// 修改系统菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost("modify")]
        [ActionPermissionFilter(Permission = "menu:edit")]
        [Log(Title = "系统菜单", BusinessType = BusinessType.UPDATE)]
        public async Task ModifyMenu([FromBody] MenuFormDto form)
        {
            await CheckFormParam(form);
            var model = form.Adapt<MenuEntity>(_mapsterAdaptEntityCfg).ToUpdate(HttpContext);
            var isOk = await _MenuService.ModifyMenu(model);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "更新失败");
            }
        }

        /// <summary>
        /// 删除系统菜单
        /// </summary>
        /// <returns></returns>
        [HttpPost("delete")]
        [ActionPermissionFilter(Permission = "menu:delete")]
        [Log(Title = "系统菜单", BusinessType = BusinessType.DELETE)]
        public async Task DeleteMenu([FromBody] DeleteMenuReq req)
        {
            var idArr = Tools.SplitAndConvert<long>(req.Ids);
            var isOk = await _MenuService.DeleteMenu(idArr, false);
            if (!isOk)
            {
                throw new CustomException(ResultCode.FAIL, "删除失败");
            }
        }





        /// <summary>
        /// 查询系统菜单树选择列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("treeSelectList")]
        [ActionPermissionFilter(Permission = "menu:treeSelectList")]
        public async Task<List<TreeNodeInfo>> GetMenuTreeSelectList([FromBody] GetMenuTreeSelectListReq req)
        {
            var result = await _MenuService.GetMenuTreeSelectList(req);
            return result;
        }


        /// <summary>
        /// 查询系统菜单分页列表
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        [HttpPost("pageList")]
        [ActionPermissionFilter(Permission = "menu:list")]
        public async Task<PagedInfo<MenuListItemDto>> GetMenuPageList([FromBody] GetMenuPageListReq req)
        {
            var result = await _MenuService.GetMenuPageList(req);
            return result;
        }

        /// <summary>
        /// 查询系统菜单详情
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet("detail/{id}")]
        [ActionPermissionFilter(Permission = "menu:detail")]
        public async Task<MenuDetailDto> GetMenuDetail(long id)
        {
            var result = await _MenuService.GetMenuDetail(id);
            return result;
        }

    }
}